package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.service.IStudentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * studentController
 * 
 * @author cww
 * @date 2021-08-17
 */
@Api(tags = "studentController")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/student" )
public class StudentController extends BaseController {

    private final IStudentService iStudentService;

    /**
     * 查询student列表
     */
    @ApiOperation(value = "查询" , notes = "查询student列表")  
    @GetMapping("/list")
    public TableDataInfo list(Student student)
    {
        startPage();
        QueryWrapper<Student> lqw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(student.getName())){
            lqw.lambda().like(Student::getName ,student.getName());
        }
        if (student.getAge() != null){
            lqw.lambda().eq(Student::getAge ,student.getAge());
        }
        if (StringUtils.isNotBlank(student.getSex())){
            lqw.lambda().eq(Student::getSex ,student.getSex());
        }
        if (StringUtils.isNotBlank(student.getSubject())){
            lqw.lambda().eq(Student::getSubject ,student.getSubject());
        }
        if (student.getScore() != null){
            lqw.lambda().eq(Student::getScore ,student.getScore());
        }
        List<Student> list = iStudentService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出student列表
     */
    @ApiOperation(value = "导出" , notes = "导出student列表")
    
    @Log(title = "student" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Student student) {
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<Student>(student);
        List<Student> list = iStudentService.list(lqw);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student. class);
        return util.exportExcel(list, "student" );
    }

    /**
     * 获取student详细信息
     */
    @ApiOperation(value = "详细信息" , notes = "获取student详细信息")
   
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iStudentService.getById(id));
    }

    /**
     * 新增student
     */
    @ApiOperation(value = "新增" , notes = "新增student")
   
    @Log(title = "student" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student) {
        return toAjax(iStudentService.save(student) ? 1 : 0);
    }

    /**
     * 修改student
     */
    @ApiOperation(value = "修改" , notes = "修改student")
   
    @Log(title = "student" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student) {
        return toAjax(iStudentService.updateById(student) ? 1 : 0);
    }

    /**
     * 删除student
     */
    @ApiOperation(value = "删除" , notes = "删除student")
  
    @Log(title = "student" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iStudentService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
